/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.audit.server  
 */
package com.apache.audit.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.audit.entity.LogModel;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.entity.LogOperation;
import com.apache.audit.entity.LogOperationDetail;
import com.apache.audit.manager.LogModelDetailManager;
import com.apache.audit.manager.LogModelManager;
import com.apache.audit.manager.LogOperationDetailManager;
import com.apache.audit.manager.LogOperationManager;
import com.apache.cache.util.Validator;
import com.apache.client.UctCoreClient;
import com.apache.database.constant.SpringContextLoader;
import com.apache.uct.common.entity.User;

/**
 * description:  日志保存辅助类
 * @author Hou Dayu 创建时间：2015-7-7  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class LoggerSaveHelper {

	private Logger log = Logger.getLogger(LoggerSaveHelper.class);
	private static LoggerSaveHelper helper;

	private LogOperationDetailManager logOperationDetailManager;
	private LogOperationManager logOperationManager;
	private LogModelManager logModelManager;
	private LogModelDetailManager logModelDetailManager;

	private LoggerSaveHelper() {
	}

	public static LoggerSaveHelper newIntance() {
		if (null == helper) {
			helper = new LoggerSaveHelper();
		}
		return (helper);
	}

	/**
	 * description:  按类名获取模型
	 * @param className
	 * @return  
	 */
	public LogModel getLogModel(String className) {
		if (Validator.isNull(className))
			return null;
		LogModel model = new LogModel();
		model.setModelEname(className);
		ParamsVo<LogModel> vo = new ParamsVo<LogModel>();
		vo.setObj(model);
		List<LogModel> list = getLogModelManager().getList(vo);
		if (!Validator.isEmpty(list))
			return list.get(0);
		return null;
	}

	/**
	 * description:  组织日志对象信息
	 */
	public LogOperation getUserByEname(LogOperation operation) {
		try {
			ResultEntity result = UctCoreClient.getUserByProperty("userEname", operation.getCreateUser());
			if ("true".equals(result.getResult()) && !Validator.isEmpty(result.getEntity())) {
				User user = (User) result.getEntity();
				operation.setOrgId(user.getOrgId());
				operation.setOrgEname(user.getOrgEname());
			}
		} catch (Exception e) {
			log.error("获取用户所在机构信息出错", e);
		}
		return operation;
	}

	public void logSave(LogOperation operation, Map beanInfo) {
		ParamsVo vo = new ParamsVo();
		String logId = Validator.generate();
		String primaryKey = "";
		if (!beanInfo.isEmpty()) {
			LogModelDetail detail = new LogModelDetail();
			detail.setModelId(operation.getModelId());
			vo.setObj(detail);
			List<LogModelDetail> list = getLogModelDetailManager().getList(vo);
			if (!Validator.isEmpty(list)) {
				List<LogOperationDetail> ods = new ArrayList<LogOperationDetail>();
				for (LogModelDetail ld : list) {
					String key = ld.getColumnEname().toLowerCase();
					String value = String.valueOf(beanInfo.get(key));
					if (Validator.isNotNull(value)) {
						LogOperationDetail od = new LogOperationDetail();
						od.setDetailId(Validator.generate());
						od.setPropertyEname(ld.getColumnEname());
						od.setPropertyName(ld.getColumnCname());
						od.setPropertyValue(value);
						od.setLogId(logId);
						od.setDetailId(Validator.generate());
						od.setPrimaryKey(ld.getPrimaryKey());
						if ("1".equals(ld.getPrimaryKey())) {
							primaryKey = value;
						}
						ods.add(od);
					}
				}
				vo.setObj(null);
				vo.setObjs(ods);
				getLogOperationDetailManager().saveInfo(vo);
			}
		}
		operation.setLogId(logId);
		operation.setPrimaryKey(primaryKey);
		vo.setObj(operation);
		getLogOperationManager().saveInfo(vo);
	}

	/**
	 * description:   记录删除信息日志 
	 * @update 2015-4-10
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void logDeleteInfo(String infoId, LogOperation operation) {
		ParamsVo vo = new ParamsVo();
		vo.setObj(operation);
		String logId = getLogOperationManager().saveInfo(vo);
		if (Validator.isNotNull(logId)) {
			LogOperationDetail detail = new LogOperationDetail();
			detail.setDetailId(Validator.generate());
			detail.setPropertyEname("infoId");
			detail.setPropertyName("主键");
			detail.setPropertyValue(infoId);
			detail.setLogId(logId);
			vo.setObj(detail);
			getLogOperationDetailManager().saveInfo(vo);
		}
	}

	private LogOperationDetailManager getLogOperationDetailManager() {
		if (null == logOperationDetailManager) {
			logOperationDetailManager = (LogOperationDetailManager) SpringContextLoader
					.getBean("logOperationDetailManager");
		}
		return logOperationDetailManager;
	}

	private LogOperationManager getLogOperationManager() {
		if (null == logOperationManager) {
			logOperationManager = (LogOperationManager) SpringContextLoader.getBean("logOperationManager");
		}
		return logOperationManager;
	}

	private LogModelManager getLogModelManager() {
		if (null == logModelManager) {
			logModelManager = (LogModelManager) SpringContextLoader.getBean("logModelManager");
		}
		return logModelManager;
	}

	private LogModelDetailManager getLogModelDetailManager() {
		if (null == logModelDetailManager) {
			logModelDetailManager = (LogModelDetailManager) SpringContextLoader.getBean("logModelDetailManager");
		}
		return logModelDetailManager;
	}
}
